{extend name="tpl/site_0/default/pc/public/layout.html" /}
{block name="title"}{$webConfig.WEBNAME}{/block}
{block name="head"}
<script src="/public/plugins/template/template.js"></script>
<script src="/public/plugins/jsAddress/jsAddress.js"></script>
<script src="/public/plugins/layer/layer.js"></script>
<script src="/public/plugins/json2.js"></script>
{/block}
{block name="body"}
<div class="container">
    <form id="orderForm">
        <input type="hidden" name="sku_ids" value="{$skuIds}"/>
        <h3>收货地址管理</h3>
        <button type="button" onclick="addAddress();" class="btn">新增收货地址</button>
        <div id="ajaxAddressList"></div>

        <h3>物流方式</h3>
        {volist name="delivery" id="vo"}
        <label><input type="radio" name="delivery_id" onclick="getFreight();" value="{$vo.delivery_id}" {eq name="i"
                      value="1" }checked{/eq} /> {$vo.delivery_name}</label>
        {/volist}
        <h3>【{$cartList.site_name}】购买商品列表</h3>
        {volist name="cartList.item_list" id="vo"}
        商品名称：{$vo.fulltitle}---购买数量：{$vo.num}---单价{$vo.simple_price}--赠送积分{$vo.give_point}分--消费积分{$vo.goods_point}分<br/>
        {/volist}
        <strong>
            合计：
            商品数量：{$cartList.total_num}
            总价格：{$cartList.total_price}元
            总赠送积分：{$cartList.total_give_point}分
            总消费积分{$cartList.total_goods_point}分
            {foreach $skuIdsWeight as $key=>$value}
            <input type="hidden" name="skuids_weight[{$key}]" value="{$value}"/>
            {/foreach}
            运费:<span id="freight">0</span>
        </strong>

        <h3>支付方式管理</h3>
        {volist name="payment" id="vo"}
        <label><input type="radio" name="pay_code" value="{$vo.pay_code}" {eq name="i" value="1" }checked{/eq} />
            {$vo.pay_name}</label>
        {/volist}

        <h3>发票信息管理</h3>
        <label><input type="radio" name="invType" value="0" checked /> 不开发票</label>
        <label><input type="radio" name="invType" value="1" /> 增值税普通发票</label>
        <label><input type="radio" name="invType" value="2" /> 增值税专用发票</label>
        <div id="invInfo"></div>

        <h3>可使用优惠券列表</h3>
        <select name="card_key">
            <option value="">选择优惠券</option>
            {volist name="couponList" id="vo"}
            <option value="{$vo.card_key}">{$vo.title}</option>
            {/volist}
        </select>
    </form>
</div>
<script id="AddressListTpl" type="text/html">
    {{each list as value index}}
    <label>
        <input type="radio" name="address_id" value="{{ value.address_id }}" {{if value.is_default==1}}checked{{/if }} />
        {{ value.address_text }} {{ value.address }} {{ value.consignee }} {{ value.tel_phone }}
    </label>
    <a href="javascript:;" onclick="editAddress(this);" data-info="{{ value.json }}">编辑</a> <br/>
    {{ /each }}
</script>
<script id="AddressFormTpl" type="text/html">
    <div style="padding:10px;">
    <form id="addressForm">
        {{ if info.address_id>0 }}
            <input type="hidden" name="address_id" class="edit-address-id" value="{{ info.address_id }}" />
        {{ /if }}
        <input type="hidden" name="is_default" value="{{ info.is_default }}" />
        <div>
            <label>收货人</label>
            <input name="consignee" type="text" class="form-control" value="{{ info.consignee }}" placeholder="收货人姓名">
        </div>
        <div>
            <label>联系电话</label>
            <input name="tel_phone" type="text" class="form-control" value="{{ info.tel_phone }}" placeholder="收货人联系方式">
        </div>
        <div>
            <label>邮编</label>
            <input name="zip_code" type="text" class="form-control" value="{{ info.zip_code }}" placeholder="收货邮编">
        </div>
        <div>
            <label>所在区域</label>
            <div class="area_name">
                <select id="prov_id" name="prov_id" class="form-control"></select>
                <select id="city_id" name="city_id" class="form-control"></select>
                <select id="dist_id" name="dist_id" class="form-control"></select>
            </div>
        </div>
        <div>
            <label>详细地址</label>
            <input name="address" class="form-control" value="{{ info.address }}" type="text" placeholder="请输入详细地址">
        </div>
        <div class="pure-controls">
            <button type="button" class="btn" onclick="saveAddress()" >
                保存收货人信息
            </button>
            <span id="validError"></span>
        </div>
    </form>
    </div>
</script>
{/block}
{block name="script"}
<script>
    $(function () {
        getFreight();
        getAddressList();
    });
    //添加收货地址
    function addAddress() {
        var html = template('AddressFormTpl',{info:{is_default:1}});
        layer.open({
            type: 1,
            title: '添加收货地址',
            area: ['800px', '500px'], //宽高
            content: html,
            success: function(layero, index){
                addressInit('prov_id', 'city_id', 'dist_id');
            }
        });
    }
    function editAddress(obj) {
        var info = $(obj).data('info');
        var html = template('AddressFormTpl',{info:info});
        layer.open({
            type: 1,
            title: '添加收货地址',
            area: ['800px', '500px'], //宽高
            content: html,
            success: function(layero, index){
                addressInit('prov_id', 'city_id', 'dist_id',info.prov_id,info.city_id,info.dist_id);
            }
        });
    }
    //保存收货地址
    function saveAddress() {
        var params = $('#addressForm').serialize();
        var addressId = parseInt($('.edit-address-id').val());
        if(addressId>0){
            var url = "{:url('home/User/addressEdit')}";
        }else{
            var url = "{:url('home/User/addressAdd')}";
        }
        $.post(url,params, function (res) {
            if(res.code==1){
                layer.closeAll('page');
                getAddressList();
            }else{
                $('#validError').text(res.msg);
            }
        });
    }
    //收货地址列表
    function getAddressList() {
        $.post("{:url('home/User/addressList')}", function (data) {
            var html = template('AddressListTpl', {list: data});
            $('#ajaxAddressList').html(html);
        });
    }
    //获取计算后得运费
    function getFreight() {
        $.post("{:url('home/Cart/getDeliveryprice')}", $('#orderForm').serialize(), function (freight) {
            $('#freight').text(freight);
        });
    }
</script>
{/block}